﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FerrScript.Builder;
using System.IO;

namespace FerrLispBuilderTester
{
    enum StateThingie
    {
        OnLoad,
        OnUpdate,
        OnDraw,
        Global
    }

    public partial class Form1 : Form
    {
        List<BuilderBlock> onLoadBlocks;
        List<BuilderBlock> onUpdateBlocks;
        List<BuilderBlock> onDrawBlocks;
        List<BuilderBlock> globalBlocks;

        StateThingie currentState = StateThingie.OnLoad;

        void UpdateFromControl()
        {
            switch (currentState)
            {
                case StateThingie.OnLoad:
                    {
                        onLoadBlocks = ferrLispBuilderControl1.GetBlockList();
                    } break;
                case StateThingie.OnDraw:
                    {
                        onDrawBlocks = ferrLispBuilderControl1.GetBlockList();
                    } break;
                case StateThingie.OnUpdate:
                    {
                        onUpdateBlocks = ferrLispBuilderControl1.GetBlockList();
                    } break;

                case StateThingie.Global:
                    {
                        globalBlocks = ferrLispBuilderControl1.GetBlockList();
                    } break;
            }
        }

        public Form1()
        {
            InitializeComponent();

            onDrawBlocks = new List<BuilderBlock>();
            onLoadBlocks = new List<BuilderBlock>();
            onUpdateBlocks = new List<BuilderBlock>();
            globalBlocks = new List<BuilderBlock>();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            StringBuilder str = new StringBuilder();

            str.Append(ScriptBuilder.Instance.BuildScript(globalBlocks));
            str.AppendLine();
            str.Append(ScriptBuilder.Instance.BuildFunction("OnLoad", null, onLoadBlocks));
            str.AppendLine();
            str.Append(ScriptBuilder.Instance.BuildFunction("OnUpdate", null, onUpdateBlocks));
            str.AppendLine();
            str.Append(ScriptBuilder.Instance.BuildFunction("OnDraw", null, onDrawBlocks));
            str.AppendLine();

            StreamWriter writer = new StreamWriter("C:\\Temp\\script.txt");
            writer.Write(str);
            writer.Close();

            System.Diagnostics.Process.Start("C:\\Temp\\script.txt");
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            UpdateFromControl();
            currentState = StateThingie.OnLoad;
            ferrLispBuilderControl1.SetBlockList(onLoadBlocks);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateFromControl();
            currentState = StateThingie.OnUpdate;
            ferrLispBuilderControl1.SetBlockList(onUpdateBlocks);
        }

        private void btnDraw_Click(object sender, EventArgs e)
        {
            UpdateFromControl();
            currentState = StateThingie.OnDraw;
            ferrLispBuilderControl1.SetBlockList(onDrawBlocks);
        }

        private void btnGlobal_Click(object sender, EventArgs e)
        {
            UpdateFromControl();
            currentState = StateThingie.Global;
            ferrLispBuilderControl1.SetBlockList(globalBlocks);
        }
    }
}
